package ar.edu.unsam.algo3.plantasvszombies.wicket;

import ar.edu.unsam.algo3.plantasvszombies.dominio.Jardin;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Mejora;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Planta;
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.JardinAppModel;
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.JardinZenAppModel;
import ar.edu.unsam.algo3.plantasvszombies.wicket.MainPage;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure0;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.uqbar.commons.model.UserException;
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods;
import org.uqbar.wicket.xtend.XAjaxLink;
import org.uqbar.wicket.xtend.XLink;
import org.uqbar.wicket.xtend.XListView;

@SuppressWarnings("all")
public class JardinZenPage extends MainPage {
  @Extension
  private WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new Function0<WicketExtensionFactoryMethods>() {
    public WicketExtensionFactoryMethods apply() {
      WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new WicketExtensionFactoryMethods();
      return _wicketExtensionFactoryMethods;
    }
  }.apply();
  
  private JardinZenAppModel _zenApp = new Function0<JardinZenAppModel>() {
    public JardinZenAppModel apply() {
      JardinZenAppModel _jardinZenAppModel = new JardinZenAppModel();
      return _jardinZenAppModel;
    }
  }.apply();
  
  public JardinZenAppModel getZenApp() {
    return this._zenApp;
  }
  
  public void setZenApp(final JardinZenAppModel zenApp) {
    this._zenApp = zenApp;
  }
  
  private FeedbackPanel feedbackPanel;
  
  public JardinZenPage(final JardinAppModel unJardinApp) {
    this.setJardinApp(unJardinApp);
    JardinZenAppModel _zenApp = this.getZenApp();
    CompoundPropertyModel<JardinZenAppModel> _compoundPropertyModel = new CompoundPropertyModel<JardinZenAppModel>(_zenApp);
    this.setDefaultModel(_compoundPropertyModel);
    Panel _menuNavegacion = this.menuNavegacion();
    this._wicketExtensionFactoryMethods.addChild(this, _menuNavegacion);
    FeedbackPanel _feedbackPanel = new FeedbackPanel("feedbackPanel");
    this.feedbackPanel = _feedbackPanel;
    this.feedbackPanel.setOutputMarkupId(true);
    JardinZenAppModel _zenApp_1 = this.getZenApp();
    JardinAppModel _jardinApp = this.getJardinApp();
    Jardin _jardin = _jardinApp.getJardin();
    _zenApp_1.setJardin(_jardin);
    this.agregarAcciones();
    Form<JardinZenAppModel> _formMejoras = this.formMejoras();
    this._wicketExtensionFactoryMethods.addChild(this, _formMejoras);
    Form<JardinZenAppModel> _formPlantas = this.formPlantas();
    this._wicketExtensionFactoryMethods.addChild(this, _formPlantas);
    JardinZenAppModel _zenApp_2 = this.getZenApp();
    _zenApp_2.actualizar();
  }
  
  public Form<JardinZenAppModel> formMejoras() {
    Form<JardinZenAppModel> _xblockexpression = null;
    {
      JardinZenAppModel _zenApp = this.getZenApp();
      CompoundPropertyModel<JardinZenAppModel> _compoundPropertyModel = new CompoundPropertyModel<JardinZenAppModel>(_zenApp);
      Form<JardinZenAppModel> _form = new Form<JardinZenAppModel>("formMejoras", _compoundPropertyModel);
      final Form<JardinZenAppModel> formMejoras = _form;
      XListView<Mejora> _xListView = new XListView<Mejora>("mejorasPosibles");
      final XListView<Mejora> listView = _xListView;
      final Procedure1<ListItem<Mejora>> _function = new Procedure1<ListItem<Mejora>>() {
        public void apply(final ListItem<Mejora> item) {
          Mejora _modelObject = item.getModelObject();
          CompoundPropertyModel<Mejora> _asCompoundModel = JardinZenPage.this._wicketExtensionFactoryMethods.<Mejora>asCompoundModel(_modelObject);
          item.setModel(_asCompoundModel);
          Label _label = new Label("descripcion");
          JardinZenPage.this._wicketExtensionFactoryMethods.addChild(item, _label);
          Label _label_1 = new Label("costo");
          JardinZenPage.this._wicketExtensionFactoryMethods.addChild(item, _label_1);
          XAjaxLink<Object> _xAjaxLink = new XAjaxLink<Object>("seleccionar");
          final Procedure1<XAjaxLink<Object>> _function = new Procedure1<XAjaxLink<Object>>() {
            public void apply(final XAjaxLink<Object> it) {
              final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
                public void apply(final AjaxRequestTarget target) {
                  JardinZenAppModel _zenApp = JardinZenPage.this.getZenApp();
                  Mejora _modelObject = item.getModelObject();
                  _zenApp.setMejoraSeleccionada(_modelObject);
                  try {
                    JardinZenAppModel _zenApp_1 = JardinZenPage.this.getZenApp();
                    _zenApp_1.mejorar();
                    it.setResponsePage(JardinZenPage.this);
                  } catch (final Throwable _t) {
                    if (_t instanceof UserException) {
                      final UserException e = (UserException)_t;
                      String _message = e.getMessage();
                      formMejoras.error(_message);
                      target.add(JardinZenPage.this.feedbackPanel);
                    } else {
                      throw Exceptions.sneakyThrow(_t);
                    }
                  }
                }
              };
              it.setOnClick(_function);
            }
          };
          XAjaxLink<Object> _doubleArrow = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink, _function);
          JardinZenPage.this._wicketExtensionFactoryMethods.addChild(item, _doubleArrow);
        }
      };
      listView.setPopulateItem(_function);
      this._wicketExtensionFactoryMethods.addChild(formMejoras, listView);
      this._wicketExtensionFactoryMethods.addChild(formMejoras, this.feedbackPanel);
      PropertyModel<Object> _propertyModel = new PropertyModel<Object>(this, "zenApp.jardin.recursos");
      Label _label = new Label("recursos", _propertyModel);
      this._wicketExtensionFactoryMethods.addChild(formMejoras, _label);
      _xblockexpression = (formMejoras);
    }
    return _xblockexpression;
  }
  
  public Object agregarAcciones() {
    return null;
  }
  
  public Form<JardinZenAppModel> formPlantas() {
    Form<JardinZenAppModel> _xblockexpression = null;
    {
      Form<JardinZenAppModel> _form = new Form<JardinZenAppModel>("formPlantas");
      final Form<JardinZenAppModel> formPlantas = _form;
      XListView<Planta> _xListView = new XListView<Planta>("result");
      final XListView<Planta> listView = _xListView;
      final Procedure1<ListItem<Planta>> _function = new Procedure1<ListItem<Planta>>() {
        public void apply(final ListItem<Planta> item) {
          Planta _modelObject = item.getModelObject();
          CompoundPropertyModel<Planta> _asCompoundModel = JardinZenPage.this._wicketExtensionFactoryMethods.<Planta>asCompoundModel(_modelObject);
          item.setModel(_asCompoundModel);
          Label _label = new Label("nombre");
          JardinZenPage.this._wicketExtensionFactoryMethods.addChild(item, _label);
          Label _label_1 = new Label("defensa");
          JardinZenPage.this._wicketExtensionFactoryMethods.addChild(item, _label_1);
          Label _label_2 = new Label("ataque");
          JardinZenPage.this._wicketExtensionFactoryMethods.addChild(item, _label_2);
          Label _label_3 = new Label("descripcion");
          JardinZenPage.this._wicketExtensionFactoryMethods.addChild(item, _label_3);
          XAjaxLink<Object> _xAjaxLink = new XAjaxLink<Object>("mejorar");
          final Procedure1<XAjaxLink<Object>> _function = new Procedure1<XAjaxLink<Object>>() {
            public void apply(final XAjaxLink<Object> it) {
              final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
                public void apply(final AjaxRequestTarget target) {
                  JardinZenAppModel _zenApp = JardinZenPage.this.getZenApp();
                  Planta _modelObject = item.getModelObject();
                  _zenApp.setPlantaSeleccionada(_modelObject);
                  target.appendJavaScript("$(\'#myModal\').modal(\'show\')");
                }
              };
              it.setOnClick(_function);
            }
          };
          XAjaxLink<Object> _doubleArrow = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink, _function);
          JardinZenPage.this._wicketExtensionFactoryMethods.addChild(item, _doubleArrow);
        }
      };
      listView.setPopulateItem(_function);
      this._wicketExtensionFactoryMethods.addChild(formPlantas, listView);
      Label _label = new Label("jardinZen.tipo");
      this._wicketExtensionFactoryMethods.addChild(formPlantas, _label);
      XLink<Object> _xLink = new XLink<Object>("siguienteZen");
      final Procedure1<XLink<Object>> _function_1 = new Procedure1<XLink<Object>>() {
        public void apply(final XLink<Object> it) {
          final Procedure0 _function = new Procedure0() {
            public void apply() {
              JardinZenAppModel _zenApp = JardinZenPage.this.getZenApp();
              _zenApp.siguienteZen();
            }
          };
          it.setOnClick(_function);
        }
      };
      XLink<Object> _doubleArrow = ObjectExtensions.<XLink<Object>>operator_doubleArrow(_xLink, _function_1);
      this._wicketExtensionFactoryMethods.addChild(formPlantas, _doubleArrow);
      _xblockexpression = (formPlantas);
    }
    return _xblockexpression;
  }
}
